package com.example.demo.controller.data;

import com.example.demo.controller.Init;
import com.example.demo.entity.Data;
import com.example.demo.enums.View;
import com.example.demo.service.IDataService;
import com.example.demo.util.NavUtil;
import de.felixroske.jfxsupport.FXMLController;
import javafx.event.Event;
import javafx.fxml.FXML;
import javafx.scene.control.TextField;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @Author hweiyu
 * @Description
 * @Date 2022/6/23 12:12
 */
@FXMLController
public class DataUpdateController implements Init {

    @Autowired
    private IDataService dataService;

    @FXML
    private TextField topic;

    @FXML
    private TextField content;

    @FXML
    private TextField remark;

    @Override
    public void init() {
        String id = NavUtil.getParameter(View.DATA_UPDATE);
        Data entity = dataService.getById(id);
        topic.setText(entity.getTopic());
        content.setText(entity.getContent());
        remark.setText(entity.getRemark());
    }

    @FXML
    public void doSubmit(Event event) {
        Data entity = new Data();
        entity.setId(Long.valueOf(NavUtil.getParameter(View.DATA_UPDATE)));
        entity.setTopic(topic.getText());
        entity.setContent(content.getText());
        entity.setRemark(remark.getText());
        dataService.updateById(entity);

        NavUtil.close(View.DATA_UPDATE);
        NavUtil.refreshTableView(View.DATA_INDEX);
    }

}
